package com.v5ba.service.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.v5ba.entity.User;
import com.v5ba.mapper.UserMapper;
import com.v5ba.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserService userService;

    @Override
    public void noTx() {
        User user = this.getById(1L);
        user.setAge(user.getAge()+1);
        this.updateById(user);
        userService.updateFailMethod();
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public void update1() {
        User user = this.getById(1L);
        user.setAge(user.getAge()+1);
        this.updateById(user);
        this.updateFailMethod();
        int i = 1 / 0;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public void updateNewTx() {
        User user = this.getById(1L);
        user.setAge(user.getAge()+1);
        this.updateById(user);
        updateFailMethod();
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRES_NEW)
    public void updateFailMethod() {
        User user = this.getById(2L);
        user.setAge(user.getAge()+1);
        this.updateById(user);

    }
}
